﻿using UnityEngine;
using System.Collections;

public class LegionTouchCamMouse : MonoBehaviour
{
	public KeyCode MouseOrbitButton;
	
	public bool AllowScreenEdgeMove;
	public bool ScreenEdgeMoveBreaksFollow;
	public int ScreenEdgeBorderWidth;
	public float MoveSpeed;
	
	public bool AllowPan;
	public bool PanBreaksFollow;
	public float PanSpeed;
	
	public bool AllowRotate;
	public float RotateSpeed;
	
	public bool AllowTilt;
	public float TiltSpeed;
	
	public bool AllowZoom;
	public float ZoomSpeed;
	
	public string RotateInputAxis = "Mouse X";
	public string TiltInputAxis = "Mouse Y";
	public string ZoomInputAxis = "Mouse ScrollWheel";
	public KeyCode PanKey1 = KeyCode.LeftShift;
	public KeyCode PanKey2 = KeyCode.RightShift;
	
	//
	
	private LegionTouchCam _cam;
	
	//
	
	public void Reset()
	{
		MouseOrbitButton = KeyCode.Mouse1;    // middle mouse by default (probably should not use right mouse since it doesn't work well in browsers)
		
		AllowScreenEdgeMove = false;
		ScreenEdgeMoveBreaksFollow = true;
		ScreenEdgeBorderWidth = 4;
		MoveSpeed = 40f;
		
		AllowPan = true;
		PanBreaksFollow = true;
		PanSpeed = 80f;
		PanKey1 = KeyCode.LeftShift;
		PanKey2 = KeyCode.Mouse0;
		
		AllowRotate = true;
		RotateSpeed = 1000;
		
		AllowTilt = true;
		TiltSpeed = 1000;
		
		AllowZoom = true;
		ZoomSpeed = 1000;
		
		RotateInputAxis = "Mouse X";
		TiltInputAxis = "Mouse Y";
		ZoomInputAxis = "Mouse ScrollWheel";
	}
	
	protected void Start()
	{
		_cam = gameObject.GetComponent<LegionTouchCam>();
	}
	
	protected void Update()
	{
		if (_cam == null)
			return; // no camera, bail!

		if(CamTakeSetting.Instance!=null && !CamTakeSetting.Instance.allowTouchCam)
			return;
		
		if (AllowZoom)
		{
			var scroll = Input.GetAxisRaw(ZoomInputAxis);
			if(scroll!=0) {
				_cam.ChangeZoom();
				float deltaZoom = scroll * ZoomSpeed * Time.deltaTime;
				float resultZoom = _cam.Distance - deltaZoom;
				if(resultZoom<= _cam.MaxDistance && resultZoom>=_cam.MinDistance)
					_cam.Distance = resultZoom;
			}
		}
		
		if (Input.GetKey(MouseOrbitButton))
		{
			if (AllowPan && (Input.GetKey(PanKey1) || Input.GetKey(PanKey2)))
			{
				// pan
				var panX = -1 * Input.GetAxisRaw("Mouse X") * PanSpeed * Time.deltaTime;
				var panZ = -1 * Input.GetAxisRaw("Mouse Y") * PanSpeed * Time.deltaTime;
				
				_cam.AddToPosition(panX, 0, panZ);
				
				if (PanBreaksFollow && (Mathf.Abs(panX) > 0.001f || Mathf.Abs(panZ) > 0.001f))
				{
					_cam.EndFollow();
				}
			}
			else
			{
				// orbit
				
				if (AllowTilt)
				{
					var tilt = Input.GetAxisRaw(TiltInputAxis);
					_cam.ChangeTilt();
					float deltaTilt = tilt * TiltSpeed * Time.deltaTime;
					float resultTilt = _cam.Tilt - deltaTilt;
					if(resultTilt>=_cam.MinTilt && resultTilt <= _cam.MaxTilt)
						_cam.Tilt = resultTilt;
				}
				
				if (AllowRotate)
				{
					var rot = Input.GetAxisRaw(RotateInputAxis);
					_cam.ChangeRotation();
					_cam.Rotation += rot * RotateSpeed * Time.deltaTime;
				}
			}
		}
		
		if (AllowScreenEdgeMove && (!_cam.IsFollowing || ScreenEdgeMoveBreaksFollow))
		{
			var hasMovement = false;
			
			if (Input.mousePosition.y > (Screen.height - ScreenEdgeBorderWidth))
			{
				hasMovement = true;
				_cam.AddToPosition(0, 0, MoveSpeed * Time.deltaTime);
			}
			else if (Input.mousePosition.y < ScreenEdgeBorderWidth)
			{
				hasMovement = true;
				_cam.AddToPosition(0, 0, -1 * MoveSpeed * Time.deltaTime);
			}
			
			if (Input.mousePosition.x > (Screen.width - ScreenEdgeBorderWidth))
			{
				hasMovement = true;
				_cam.AddToPosition(MoveSpeed * Time.deltaTime, 0, 0);
			}
			else if (Input.mousePosition.x < ScreenEdgeBorderWidth)
			{
				hasMovement = true;
				_cam.AddToPosition(-1 * MoveSpeed * Time.deltaTime, 0, 0);
			}
			
			if (hasMovement && _cam.IsFollowing && ScreenEdgeMoveBreaksFollow)
			{
				_cam.EndFollow();
			}
		}
	}
}
